package com.example.yz.happywoker;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.example.yz.happywoker.HistoryOrderActivity.MyAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HistoryOrder extends AppCompatActivity implements View.OnClickListener {
    public ListView listView;
    private MyAdapter mAdapter;
    private ImageView suggestion_his;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_order);
        listView = (ListView) findViewById(R.id.list);
        suggestion_his = (ImageView) findViewById(R.id.suggestion_his);
        suggestion_his.setOnClickListener(this);
        GlobalData.list.clear();
        if (isNetworkAvailable(this)) {
            AVQuery<AVObject> query = new AVQuery<>("MyOrderList");
            AVUser currentUser = AVUser.getCurrentUser();
            Log.e("ggggggg", "" + "else也执行了" + currentUser);
            query.whereEqualTo("WokerID", currentUser.get("username"));//通过关键字查找表单
            query.findInBackground(new FindCallback<AVObject>() {
                @Override
                public void done(List<AVObject> listorder, AVException e) {
                    for (AVObject avObject : listorder) {
                        String status = avObject.getString("Status");
                        if (status.equals("4")) {
                            Map<String, Object> map = new HashMap<String, Object>();
                            map.put("RepairCategory", avObject.getString("RepairCategory"));
                            //map.put("Status", avObject.getString("Status"));
                            map.put("Status", "已完成");
                            map.put("HandleTime", avObject.getString("HandleTime"));
                            map.put("ServiceContent", avObject.getString("ServiceContent"));
                            map.put("RealAddress", avObject.getString("RealAddress"));
                            map.put("objectid", avObject.getObjectId());
                            GlobalData.list.add(map);
                            mAdapter = new MyAdapter(HistoryOrder.this, GlobalData.list);
                            listView.setAdapter(mAdapter);
                            Log.e("objectid", "" + avObject.getObjectId());
                            Log.e("HistoryOrder", "" + GlobalData.list.toString());
                        }
                    }

                }
            });
        } else {
            Toast.makeText(this, "当前无网络连接", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 检查当前网络是否可用
     *
     * @param
     * @return
     */

    private boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    //      System.out.println(i + getString(R.string.netState) + networkInfo[i].getState());
                    //    System.out.println(i + getString(R.string.netType) + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.suggestion_his:
                finish();
                break;
        }
    }
}
